package com.belf.web.controller.console;

import com.belf.common.annotation.Log;
import com.belf.common.core.controller.BaseController;
import com.belf.common.core.domain.AjaxResult;
import com.belf.common.core.domain.entity.SysUser;
import com.belf.common.core.page.TableDataInfo;
import com.belf.common.enums.BusinessType;
import com.belf.common.utils.SecurityUtils;
import com.belf.console.domain.DataReportObject;
import com.belf.console.domain.DataSqlTask;
import com.belf.console.service.IDataSqlTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * SQL执行Controller
 * 
 * @author belf
 * @date 2024-08-03
 */
@RestController
@RequestMapping("/console/sqlTask")
public class DataSqlTaskController extends BaseController
{
    @Autowired
    private IDataSqlTaskService dataSqlTaskService;

    /**
     * 查询SQL执行列表
     */
    @PreAuthorize("@ss.hasPermi('console:sqlTask:list')")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody DataSqlTask dataSqlTask)
    {
        // 管理员拥有所有权限
        if (!SysUser.isAdmin(SecurityUtils.getUserId())){
            dataSqlTask.setCreateBy(getUsername());
        }
        List<DataSqlTask> list = dataSqlTaskService.selectDataSqlTaskList(dataSqlTask);
        long count = dataSqlTaskService.selectDataCount(dataSqlTask);
        return getDataTablePost(list,count);
    }

    /**
     * 获取SQL执行详细信息
     */
    @PreAuthorize("@ss.hasPermi('console:sqlTask:query')")
    @PostMapping("/getInfo")
    @ResponseBody
    public AjaxResult getInfo(@RequestBody DataSqlTask dataSqlTask)
    {
        return success(dataSqlTaskService.selectDataSqlTaskById(dataSqlTask.getId()));
    }

    /**
     * 新增SQL执行
     */
    @PreAuthorize("@ss.hasPermi('console:sqlTask:add')")
    @Log(title = "SQL执行", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody DataSqlTask dataSqlTask)
    {
            dataSqlTask.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dataSqlTaskService.insertDataSqlTask(dataSqlTask));
    }

    /**
     * 修改SQL执行
     */
    @PreAuthorize("@ss.hasPermi('console:sqlTask:edit')")
    @Log(title = "SQL执行", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestBody DataSqlTask dataSqlTask)
    {
        dataSqlTask.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dataSqlTaskService.updateDataSqlTask(dataSqlTask));
    }

    /**
     * 删除SQL执行
     */
    @PreAuthorize("@ss.hasPermi('console:sqlTask:remove')")
    @Log(title = "SQL执行", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody DataSqlTask dataSqlTask)
    {
        return toAjax(dataSqlTaskService.deleteDataSqlTaskByIds(dataSqlTask.getIdArray()));
    }


    @Log(title = "执行sql", businessType = BusinessType.TEST)
    @PostMapping("/executeSql")
    @ResponseBody
    public AjaxResult executeSql(@RequestBody DataSqlTask dataSqlTask){
        dataSqlTask.setCreateBy(SecurityUtils.getUsername());
        return dataSqlTaskService.executeSql(dataSqlTask);
    }

    @Log(title = "执行sql", businessType = BusinessType.TEST)
    @PostMapping("/convertSql")
    @ResponseBody
    public AjaxResult convertSql(@RequestBody DataReportObject dataReportObject){
        return dataSqlTaskService.convertSql(dataReportObject);
    }
}
